home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / utils / console / splitvt-.000 / splitvt- / splitvt-1.6.1 / cut-paste.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-08  |  4.4 KB  |  207 lines

  1.  
  2. /* The cut-paste module for splitvt */
  3.  
  4. #include    <stdio.h>
  5. #include    "vt100.h"
  6. #include    "video.h"
  7.  
  8. #define Min(A,B)    ((A>B)?B:A)
  9. #define Max(A,B)    ((A>B)?A:B)
  10.  
  11. extern struct physical physical;
  12.  
  13. /* This function assumes that it alone will print selected text */
  14. unsigned char put_sel_char(win, x, y, oldattr, on)
  15. window *win;
  16. int x, y;
  17. int oldattr;
  18. unsigned char *on;
  19. {
  20.     int c;
  21.  
  22.     c = get_video(win, x, y);
  23.     if ( c&0xFF ) {
  24.         if ( ((c>>8)&SELECTED) == SELECTED ) {
  25.             c &= ~(SELECTED<<8);
  26.             if ( (*on&SELECTED) == SELECTED ) {
  27.                 vt_resetattr();
  28.                 *on = NORMAL;
  29.                 oldattr = 0;
  30.             }
  31.         } else {
  32.             c |= (SELECTED<<8);
  33.             if ( (*on&SELECTED) == SELECTED )
  34.                 *on &= ~SELECTED;
  35.         }
  36.         oldattr=check_attr(c, oldattr, on);
  37.         put_video(c, win, x, y);
  38.         printf("%c", c&0xFF);
  39.     } else
  40.         printf(" ");
  41.     fflush(stdout);
  42.     return(oldattr);
  43. }
  44.  
  45. /* Get a window selection */
  46. char *vt_getsel(win, buf, len)
  47. int win;
  48. char *buf;
  49. int len;
  50. {
  51.     int i, j;
  52.     int c, oldattr=0, marked=0;
  53.     window *thiswin;
  54.     unsigned char on=NORMAL;
  55.     position here, cursor, mark1, mark2;
  56.     position startsel, endsel;
  57.     
  58.     thiswin = physical.subwins[win];
  59.     here=cursor=thiswin->cursor;
  60.     vt_info("Mark beginning of selection: ");
  61.     while ( (c=getchar()) != EOF ) {
  62.         switch (c) {
  63.             case 'j':    /* Move down one line */
  64.                     if ( cursor.x < thiswin->rows ) {
  65.                         if ( marked ) {
  66.                             for (j=cursor.y; j<thiswin->cols; ++j) {
  67.                                 oldattr=
  68.                 put_sel_char(thiswin, cursor.x, j, oldattr, &on);
  69.                             }
  70.                             printf("\r");
  71.                             vt_down(1);
  72.                             ++cursor.x;
  73.                             for ( j=1; j<cursor.y; ++j ) {
  74.                                 oldattr=
  75.                 put_sel_char(thiswin, cursor.x, j, oldattr, &on);
  76.                             }
  77.                         } else {
  78.                             vt_down(1);
  79.                             ++cursor.x;
  80.                         }
  81.                     }
  82.                     break;
  83.             case 'k':    /* Move up one line */
  84.                     if ( cursor.x > 1 ) {
  85.                         --cursor.x;
  86.                         if ( marked ) {
  87.                             vt_up(1);
  88.                             for (j=cursor.y; j<thiswin->cols; ++j) {
  89.                                 oldattr=
  90.                 put_sel_char(thiswin, cursor.x, j, oldattr, &on);
  91.                             }
  92.                             printf("\r");
  93.                             vt_down(1);
  94.                             ++cursor.x;
  95.                             for ( j=1; j<cursor.y; ++j ) {
  96.                                 oldattr=
  97.                 put_sel_char(thiswin, cursor.x, j, oldattr, &on);
  98.                             }
  99.                             vt_up(1);
  100.                             --cursor.x;
  101.                         } else 
  102.                             vt_up(1);
  103.                     }
  104.                     break;
  105.             case 'l':    /* Move right one char */
  106.                     if ( cursor.y < thiswin->cols ) {
  107.                         if ( marked ) {
  108.                             oldattr=
  109.                 put_sel_char(thiswin, cursor.x, cursor.y, oldattr, &on);
  110.                         } else
  111.                             vt_right(1);
  112.                         ++cursor.y;
  113.                     }
  114.                     break;
  115.             case 'h':    /* Move left one char */
  116.                     if ( cursor.y > 1 ) {
  117.                         --cursor.y;
  118.                         if ( marked ) {
  119.                             vt_left(1);
  120.                             oldattr=
  121.                 put_sel_char(thiswin, cursor.x, cursor.y, oldattr, &on);
  122.                             vt_left(1);
  123.                         } else
  124.                             vt_left(1);
  125.                     }
  126.                     break;
  127.             case ' ':    /* Mark selection */
  128.                     if ( marked ) {
  129.                         mark2=cursor;
  130.                         /* Copy and deselect area */
  131.                         /* upper-left -> lower-right */
  132.                         if ( mark1.x < mark2.x )
  133.                         {
  134.                             startsel = mark1;
  135.                             endsel = mark2;
  136.                         }
  137.                         else if ( mark2.x < mark1.x )
  138.                         {
  139.                             startsel = mark2;
  140.                             endsel = mark1;
  141.                         }
  142.                         else if ( mark1.y < mark2.y )
  143.                         {
  144.                             startsel = mark1;
  145.                             endsel = mark2;
  146.                         }
  147.                         else
  148.                         {
  149.                             startsel = mark2;
  150.                             endsel = mark1;
  151.                         }
  152.                         getsel_video(thiswin, buf, len,
  153.                             startsel.x, endsel.x,
  154.                             startsel.y, endsel.y);
  155.                         clrsel_video(thiswin);
  156.                         vt_info("Region selected.");
  157.                         /* Repaint the screen */
  158.                         paint_video(thiswin);
  159.                         return(buf);
  160.                     } else {
  161.                         mark1=cursor;
  162.                         marked=1;
  163.                         vt_info(
  164.                         "Mark end of selection: ");
  165.                         /* Set reverse here */
  166.                         vt_update();
  167.                     }
  168.                     break;
  169.             case '\033':
  170.             case 'q':    /* Cancel selection */
  171.                     clrsel_video(thiswin);
  172.                     vt_goto((here.x+thiswin->row_offset),
  173.                                     here.y);
  174.                     vt_info("Selection cancelled.");
  175.                     return(NULL);
  176.                     break;
  177.             default:    break;
  178.         }
  179.     }
  180.     return;    /* Hopefully, we never reach here */
  181. }
  182.  
  183.  
  184. /* Set a window selection */
  185. char *vt_setsel(buf, len, startx, endx, starty, endy)
  186. char *buf;
  187. int len;
  188. int startx, starty;
  189. int endx, endy;
  190. {
  191.     window *thiswin;
  192.  
  193.     /* If nothing selected, return old selection */
  194.     if ( (startx == endx) && (starty == endy) )
  195.         return(buf);
  196.  
  197.     if ( startx < (physical.subwins[LOWER])->row_offset )
  198.         thiswin = physical.subwins[UPPER];
  199.     else {
  200.         thiswin = physical.subwins[LOWER];
  201.         startx-=thiswin->row_offset;
  202.         endx-=thiswin->row_offset;
  203.     }
  204.     getsel_video(thiswin, buf, len, startx, endx, starty, endy);
  205.     return(buf);
  206. }
  207.